package com.example.excel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSym;

import com.deepoove.poi.policy.AbstractRenderPolicy;
import com.deepoove.poi.policy.RenderPolicy;
import com.deepoove.poi.render.RenderContext;

/**
 * 特殊符号渲染规则
 * @author liuyake
 *${<var}  例如：${<变量名}
 * 2018-11-09
 */
public class SymbolRenderPolicy extends AbstractRenderPolicy implements RenderPolicy{
	
	private final Log log = LogFactory.getLog(this.getClass());

	@Override
	protected boolean validate(Object data) {
		if(data==null) {
			return false;
		}
		return true;
	}

	@Override
	public void doRender(RenderContext paramRenderContext) throws Exception {
		Object data = paramRenderContext.getData();
		SymbolRenderData renderData=data instanceof SymbolRenderData ? (SymbolRenderData) data : new SymbolRenderData("");
		XWPFRun run = paramRenderContext.getRun();
		CTSym sym = null; 
		try {
            sym = CTSym.Factory.parse(renderData.getText());
            //创建一个符号
            run.getCTR().addNewSym().set(sym);
        } catch (XmlException xe) {
            xe.printStackTrace();
        }
		 //清除原标记
        clearPlaceholder(paramRenderContext,true);
	}

	/*@Override
	public void doRender(RunTemplate runTemplate, Object data, XWPFTemplate template) throws Exception {
		SymbolRenderData renderData = data instanceof SymbolRenderData ? (SymbolRenderData) data : new SymbolRenderData("");
		XWPFRun run = runTemplate.getRun();
		CTSym sym = null;  
        try {
            sym = CTSym.Factory.parse(renderData.getText());
            //创建一个符号
            run.getCTR().addNewSym().set(sym);
        } catch (XmlException xe) {
            xe.printStackTrace();
        }
        //清除原标记
        clearPlaceholder(run);
	}*/


}
